var Tank = cc.Sprite.extend({
    _HP : 0,
    _speed : 0,
    ////////////////
    _xSpeed : 1,
    _ySpeed : 1,
    _direct : 0,
    //////////////////
    _life : true,
    _canMove : false,
    ///////////////////////
    ctor : function(filename,hp,speed){
        this._super(filename);
        this._HP = hp;
        this._speed = speed;
    },
    onEnter:function () {
        this._super();
    },
    onExit : function(){
        this._super();
    },
    ///////////////////////////////////////////////////////////
    getLife : function(){
        return this._life;
    },
    setLife : function(life){
        this._life=life;
    },
    ///////////////////////////////////////////////////////////
    moveState:function(state){
        this._canMove = state;
    },
    ///////////////////////////////////////////////////////////
    collideRect:function (x, y) {
        var width=80,height=80;
        return cc.rect(x-width*0.5, y-height*0.5, width, height);
    },
    ///////////////////////////////////////////////////////////
    hurt : function(hurt){
        this._HP-=hurt;
        if(this._HP<=0){
            this._HP=0;
            this.dead();
        }
    },
    ////////////////////////////////////////////////////////////
    swerve : function(dir){
        var rand = dir;
        this._xSpeed = rand<2?rand:(rand==3?-1:0);
        this._ySpeed = rand==0?1:(rand==2?-1:0);
        this._direct = rand;
        switch(rand){
            case 0:{
                this.toUp();
                break;
            }
            case 1:{
                this.toRight();
                break;
            }
            case 2:{
                this.toDown();
                break;
            }
            case 3:{
                this.toLeft();
                break;
            }
        }
        cc.log( "x speed: "+this._xSpeed );
        cc.log( "y speed: "+this._ySpeed );
        cc.log( "direct: "+this._direct );
    },
    ////////////////////// moving //////////////////////
    moving : function( dt  ){
        if( this._canMove ){
            var np = this.getPosition();
            np.x += this._xSpeed*this._speed*dt;
            np.y += this._ySpeed*this._speed*dt;
            if( !Blocked(np.x,np.y,this._direct) ){
                this.setPosition(np.x,np.y);
            }else{
                cc.log( "blocked direction" );
            }
        }
    }
});

